/*
 * 代号：凤凰
 * http://www.jphenix.org
 * 2014-06-06
 * V4.0
 */
package com.jphenix.servlet.api.javax;

import com.jphenix.share.lang.SEnumeration;
import com.jphenix.standard.docs.ClassInfo;
import com.jphenix.standard.servlet.api.IServletConfig;
import com.jphenix.standard.servlet.api.IServletContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.FilterConfig;

/**
 * 封装后的配置信息类
 * 
 * 2022-09-04 重构了对接Tomcat9以及以前版本代码
 * 
 * @author 刘虻
 * 2010-5-25 下午04:41:24
 */
@ClassInfo({"2022-09-04 21:58","封装后的配置信息类"})
public class ServletConfigImpl implements IServletConfig {

	private FilterConfig       kernel = null; //过滤器配置类
	private Map<String,String> initParamterMap = new HashMap<String,String>(); // 初始化参数容器
	
	/**
	 * 构造函数
	 * @author 刘虻
	 */
	public ServletConfigImpl(FilterConfig filterConfig) {
		super();
		this.kernel = filterConfig;
	}

	/**
	 * 覆盖方法
	 * 刘虻
	 * 2010-5-25 下午04:41:24
	 */
	@Override
    public String getInitParameter(String name) {
		if(name==null) {
			return null;
		}
		if(initParamterMap.containsKey(name)) {
			return initParamterMap.get(name);
		}
		return kernel.getInitParameter(name);
	}

	/**
	 * 覆盖方法
	 * 刘虻
	 * 2010-5-25 下午04:41:24
	 */
	@SuppressWarnings("unchecked")
	@Override
    public Enumeration<String> getInitParameterNames() {
		// 构建返回值序列
		Collection<String> reList = new ArrayList<String>();
		Enumeration<String> names = kernel.getInitParameterNames();
		String name;
		if(names!=null) {
			while(names.hasMoreElements()) {
				name = names.nextElement();
				if(name==null || reList.contains(name)) {
					continue;
				}
				reList.add(name);
			}
		}
		if(initParamterMap.size()>0) {
			Iterator<String> keyIterator = initParamterMap.keySet().iterator();
			while(keyIterator.hasNext()) {
				name = keyIterator.next();
				if(name==null || reList.contains(name)) {
					continue;
				}
				reList.add(name);
			}
		}
		;
		return new SEnumeration<String>(reList);
	}

	/**
	 * 覆盖方法
	 * 刘虻
	 * 2010-5-25 下午04:41:24
	 */
	@Override
    public IServletContext getServletContext() {
		return new ServletContextImpl(kernel.getServletContext());
	}

	/**
	 * 覆盖方法
	 * 刘虻
	 * 2010-5-25 下午04:41:24
	 */
	@Override
    public String getServletName() {
		return kernel.getFilterName();
	}

	/**
	 * 覆盖方法
	 */
	@Override
	public void setInitParameter(String key, String value) {
		initParamterMap.put(key,value);
	}
}
